package com.classes;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.mbbmap.app.manager.ConfigManager;
import com.mbbmap.app.manager.DBConnectionManager;

public class InterchangeFactory {
	private String message;
	private long outletID = 0;
	
	public String getMessage() {
		return "[InterchangeFactory] " + this.message; 
	}
	
	public long getOutletID() {
		return this.outletID; 
	}
	
	
	public Interchange getMccRate(String if_id){
        String connectionPool = ConfigManager.getInstance().getConnectionPool();
        DBConnectionManager connMgr = DBConnectionManager.getInstance();
        Connection conn = connMgr.getConnection(connectionPool, 10000);
        PreparedStatement statement =  null;
		try{
			String sql = "select ifmcc.ifm_code, ifdetails.* "
					+ "from ifmcc "
					+ "join ifdetails on ifmcc.ifm_ifid=ifdetails.if_id "
					+ "where ifm_code = ?";
			
			statement = conn.prepareStatement(sql);
			statement.setString(1, if_id);
			
			ResultSet resultSet = statement.executeQuery();
			
			while(resultSet.next()){
				Interchange i = new Interchange();
				
				i.setIfvln(resultSet.getString("if_vln"));
				i.setIfvlh(resultSet.getString("if_vlh"));
				i.setIfvlm(resultSet.getString("if_vlm"));
				i.setIfvlec(resultSet.getString("if_vlec"));
				i.setIfvfn(resultSet.getString("if_vfn"));
				i.setIfvfh(resultSet.getString("if_vfh"));
				i.setIfvfm(resultSet.getString("if_vfm"));
				i.setIfvfec(resultSet.getString("if_vfec"));
				i.setIfmcln(resultSet.getString("if_mcln"));
				i.setIfmclh(resultSet.getString("if_mclh"));
				i.setIfmclm(resultSet.getString("if_mclm"));
				i.setIfmclec(resultSet.getString("if_mclec"));
				i.setIfmcfn(resultSet.getString("if_mcfn"));
				i.setIfmcfh(resultSet.getString("if_mcfh"));
				i.setIfmcfm(resultSet.getString("if_mcfm"));
				i.setIfmcfec(resultSet.getString("if_mcfec"));
				
				return i;
			}
			
			return null;
		}
		catch (Exception e){
			this.message ="Mcc interchange rate not found";
			return null;
		} finally {
        	if (statement != null) {
        		try { statement.close(); } catch (Exception e) {}
        	}
			connMgr.freeConnection(connectionPool, conn);
		}
	
	}
	
	public Interchange getMcc(int ifm_mcc) {
        String connectionPool = ConfigManager.getInstance().getConnectionPool();
        DBConnectionManager connMgr = DBConnectionManager.getInstance();
        Connection conn = connMgr.getConnection(connectionPool, 10000);
        PreparedStatement statement = null;
		try {
			
			String sql = "select COUNT(ifm_code) as counter from ifmcc where ifm_code = ?";
			
			statement = conn.prepareStatement(sql);
			statement.setInt(1, ifm_mcc);
			
			ResultSet resultSet = statement.executeQuery();
			
			while(resultSet.next()){
				Interchange i = new Interchange();
				
				
				i.setIfmcode(Long.parseLong(resultSet.getString("counter")));
				
				return i;
			}
			
			this.message = "Mcc code not found.";
			return null;
			
		} catch (Exception e){
			this.message = e.getMessage();
			return null;
		} finally {
        	if (statement != null) {
        		try { statement.close(); } catch (Exception e) {}
        	}
			connMgr.freeConnection(connectionPool, conn);
		}
	}
	
	
}
